/**
 * Created by 97203 on 2018/06/08.
 */
$(function () {
    var dept_datagrid = $("#dept_datagrid");
    var dept_dialog = $("#dept_dialog");
    var dept_form = $("#dept_form");
    var item_dialog = $("#item_dialog")
    var item_form = $("#item_form")
    dept_datagrid.datagrid({
        url: '/damage/query.do',
        fitColumns: true,
        fit: true,
        pagination: true,    //分页条
        rownumbers: true,     //行号列
        singleSelect: true,
        toolbar: '#tb',
        columns: [[
            {
                field: 'claim', title: '保单编号', width: 100, formatter: function (value,row) {
                return row.claim ? row.claim.staging : "";
            }},
            {field: 'payee', title: '收款人', width: 100},
            {field: 'money', title: '金额', width: 100},
            {field: 'carnumber', title: '卡号', width: 100},
            {
             field: 'employee', title: '经手人', width: 100, formatter: function (value,row) {
             return row.employee ? row.employee.username : "";
             }},

            {field: 'disposeTime', title: '拨款时间', width: 100},
            {
                field: 'state', title: '状态', width: 100, formatter: function (value) {
                return value ? "<font color='green'>已拨款</font>" : "<font color='red'>未拨款</font>";
            }
            }
        ]]
    })

    item_dialog.dialog({
        width: 450,
        height: 300,
        title:'事故处理',
        buttons: '#btn',
        closed: true,
        //在窗口关闭的时候触发事件
        onClose: function () {
            //清空表单数据
            item_dialog.form('clear');
        }
    })

    var methodObj = {
        dispose: function () {

            var row = dept_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选中要编辑的数据！', 'warning');
                return;
            }
            /* $.get('/claim/getCliamItemByClaimId.do', {claimId: row.id}, function (data) {
             row["item.site"]=data.site;
             row["item.carnumber"]=data.carnumber;

             })*/
            console.log(row);

            //把数据回显到表单中
            item_form.form('load', row);
            //打开弹出框
            item_dialog.dialog('open');
        },
        save: function () {
            //提交ajax
            item_form.form('submit', {
                url: '/damage/update.do',
                success: function (data) {
                    data = $.parseJSON(data)
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功！', 'info', function () {
                            //关闭弹出框
                            methodObj.cancel();
                            //在dataGrid重新加载数据
                            dept_datagrid.datagrid('reload');
                        });
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'warning');
                        item_dialog.dialog('close');
                    }
                }
            })
        },
        reload: function () {
            dept_datagrid.datagrid('reload');
        },
        changeState: function () {
            var row = dept_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选中要操作的数据！', 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您想要执行操作吗？', function (r) {
                if (r) {
                    $.get('/damage/changeState.do', {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功！', 'info', function () {
                                dept_datagrid.datagrid('reload');
                            });
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        cancel: function () {
            item_dialog.dialog('close');
        }

    }

    $("a[data-cmd]").click(function () {
        //获取按钮上的data-cmd的值
        var methodName = $(this).data("cmd");
        methodObj[methodName]();
    })

})
